package com.frxuexi.park2209.sign

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.frxuexi.base.entity.SignBean
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209.R
import com.frxuexi.park2209.adapter.SignAdapter
import com.frxuexi.park2209.databinding.ActivitySignBinding
import com.frxuexi.park2209.databinding.ActivitySignListBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Calendar

@AndroidEntryPoint
class SignListActivity : BaseActivity<ActivitySignListBinding,SignViewModel>() {
    lateinit var signAdapter: SignAdapter
    var month=Calendar.getInstance().get(Calendar.MONTH)+1
    override fun initData() {
        lifecycleScope.launch {
            vm.getsignSuc.collect{
                if(it.isNotEmpty()){
                    val json = Gson().fromJson(it, SignBean::class.java).filter {
                        if(month<10){
                            SimpleDateFormat("yyyy-MM").format(it.startTime)==SimpleDateFormat("yyyy").format(System.currentTimeMillis())+"-0"+month
                        }else{
                            SimpleDateFormat("yyyy-MM").format(it.startTime)==SimpleDateFormat("yyyy").format(System.currentTimeMillis())+"-"+month
                        }

                    }
                    Log.i("===",json.toString())
                    signAdapter.addData(json)
                    signAdapter.notifyDataSetChanged()
                }
            }
        }
    }

    override fun initView() {
        signAdapter= SignAdapter()
        binding.rvSign.adapter=signAdapter
        binding.rvSign.layoutManager=LinearLayoutManager(this)

        binding.tvMonth.text=month.toString()
        vm.getSigns(mapOf("month" to month))

        binding.up.setOnClickListener {
            if(month>1) {
                month -= 1
                binding.tvMonth.text=month.toString()
                signAdapter.data.clear()
                vm.getSigns(mapOf("month" to month))
            }

        }

        binding.right.setOnClickListener {
            if(month<12) {
                month += 1
                binding.tvMonth.text=month.toString()
                signAdapter.data.clear()
                vm.getSigns(mapOf("month" to month))
            }
        }
    }

}